﻿using Seagull.BarTender.Print;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace TB_WinForm.HongBao
{
    public partial class HB_Batch : Form
    {
        
        public HB_Batch()
        {
            InitializeComponent();
        }
        private static string BTW_LOCAL = Application.StartupPath + @"\BarCode\" + "HB_QR.btw";
        private void HB_Batch_Load(object sender, EventArgs e)
        {
           

            textBox1.PasswordChar = '*';
            label2.Text = SysInfo.WSHBL.GET_WAIT_PRINT_COUNT(Convert.ToInt32(SysInfo.Seller_Info.Tables[0].Rows[0][0].ToString())).ToString();
            PrintAddList();//加载打印机
        }
        private void PrintAddList()
        {
            Printers printers = new Printers();
            foreach (Printer printer in printers)
            {
                comboBox1 .Items.Add(printer.PrinterName);
            }

            if (printers.Count > 0)
            {
                // Automatically select the default printer.
                comboBox1.SelectedItem = printers.Default.PrinterName;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            label2.Text = SysInfo.WSHBL.GET_WAIT_PRINT_COUNT(Convert.ToInt32(SysInfo.Seller_Info.Tables[0].Rows[0][0].ToString())).ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Thread t1 = new Thread(new ThreadStart(Create_WP_HB));
            t1.IsBackground = true;
            t1.Start();
            MessageBox.Show("后台已经开始创建红包，增加完毕后会提示您，而后点击刷新即可看到最新红包剩余数量，期间不要退出程序", "提示");
        }
        public static void Create_WP_HB()
        {
            int HB_Count = 50;
            for (int i = 0; i < HB_Count; i++)
            {
                Thread.Sleep(4000);
                SysInfo.WSHBL.INSERT_HB_SERVER(Convert.ToInt32(SysInfo.Seller_Info.Tables[0].Rows[0][0].ToString()),
                    SysInfo.WX_User.Trim().ToString(), "批量生成红包", "", 0, "Wait_Print");
            }
            MessageBox.Show(HB_Count.ToString() + " 个红包已创建完毕");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if(Convert.ToInt32(comboBox2.Text.Trim().ToString())> SysInfo.WSHBL.GET_WAIT_PRINT_COUNT(Convert.ToInt32(SysInfo.Seller_Info.Tables[0].Rows[0][0].ToString())))
            {
                //取出数量大于库存数量
                label2.Text = SysInfo.WSHBL.GET_WAIT_PRINT_COUNT(Convert.ToInt32(SysInfo.Seller_Info.Tables[0].Rows[0][0].ToString())).ToString();
                MessageBox.Show("取出数量大于红包池库存数量，请增加库存量后再取出，或减小取出数量", "警告");
            }
            else
            {
                    Thread BarCode_Print_TD = new Thread(new ThreadStart(FH_Print));
                    BarCode_Print_TD.IsBackground = true;
                    BarCode_Print_TD.Start();
            }
        }
        private void FH_Print()
        {
            DataSet HB_DST = SysInfo.WSHBL.GET_WP_HB_LIST(Convert.ToInt32(SysInfo.Seller_Info.Tables[0].Rows[0][0].ToString()), Convert.ToInt32(comboBox2.Text.Trim().ToString()));
            for (int i = 0; i < HB_DST.Tables[0].Rows.Count; i++)
            {
                string QR_Scene_ID = HB_DST.Tables[0].Rows[i][2].ToString();
                DataSet QR_DST = SysInfo.WSHBL.GET_HB_Info(QR_Scene_ID.ToString());
                string QR_URL = QR_DST.Tables[0].Rows[0][10].ToString();

                try
                {
                    LabelFormatDocument labelFormat;
                    using (Engine btEngine = new Engine(true))
                    {
                        labelFormat = btEngine.Documents.Open(BTW_LOCAL.ToString());//载入模板
                        labelFormat.SubStrings["QR_URL"].Value = QR_URL.ToString();
                        labelFormat.SubStrings["SAFECODE"].Value = QR_Scene_ID.ToString();

                        labelFormat.PrintSetup.PrinterName = comboBox1.Text.Trim().ToString();
                        labelFormat.PrintSetup.IdenticalCopiesOfLabel = 1;//打印数量
                        labelFormat.Print(QR_Scene_ID.ToString() + DateTime.Now.ToString(), 1);//开始打印
                        SysInfo.WSHBL.PRINT_NONE_HB(QR_Scene_ID.ToString(), DateTime.Now.ToString() + " 打印二维码");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            
            Thread.Sleep(2000);

        }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Enter)
            {
                if (comboBox3.Text == "")
                {
                    MessageBox.Show("请选择返现金额", "警告");
                    comboBox3.Focus();
                }
                textBox2.Focus();

            }
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string RT_Str = SysInfo.WSHBL.MATE_HB(textBox1.Text.Trim().ToString(),
                    textBox2.Text.Trim().ToString(), Convert.ToInt32(comboBox3.Text.Trim().ToString()),
                    get_FX_Type());
                if(RT_Str.Split('#')[0].ToString()=="1")
                {
                    //匹配成功
                    listBox1.Items.Add("订单号：" + textBox2.Text.Trim().ToString() + " 关联成功");
                    textBox1.Text = "";
                    textBox2.Text = "";
                    textBox1.Focus();
                    
                }
                else
                {
                    MessageBox.Show("关联失败 " + RT_Str.Split('#')[1].ToString(), "警告");
                    listBox1.Items.Add("【失败】单号：" + textBox2.Text.Trim().ToString() + " 关联失败");
                    textBox1.Text = "";
                    textBox2.Text = "";
                    textBox1.Focus();
                }
            }
        }

        private string get_FX_Type()
        {
            if(radioButton1.Checked)
            {
                return "UN_Collect";
            }
            else if(radioButton2.Checked)
            {
                return "Wait_Collect";
            }
            else if(radioButton3.Checked)
            {
                return "Wait_Collect_Rate_2";
            }
            else
            {
                return "Error";
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void HB_Batch_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }
    }
}
